/**
 * 访问"书架"接口网络模块
 */
import axios from "./axios.config";
import util from "./util";

class CakeShelfApi {
  async findAll(limit, offset, orderBy, sort) {
    let result = await axios.get(
      `/cakeshelves?limit=${limit}&offset=${offset}&order_by=${orderBy}&sort=${sort}`
    );
    return util.jsonTransfer(result.data);
  }

  async getCount() {
    let result = await axios.get(`/cakeshelves/count`);
    return util.jsonTransfer(result.data);
  }

  async add(cakeId) {
    let result = await axios.post(`/cakeshelves`, { cakeId: cakeId });
    return util.jsonTransfer(result.data);
  }

  // async edit(cake) {
  //   let result = await axios.put(`/cakes/${cake.id}`, cake);
  //   return util.jsonTransfer(result.data);
  // }

  async delete(id) {
    let result = await axios.delete(`/cakeshelves/${id}`);
    return util.jsonTransfer(result.data);
  }

  // async search(keyword) {
  //   let result = await axios.get(`/cakes/search?q=${keyword}`);
  //   return util.jsonTransfer(result.data);-
  // }

  async findByCakeId(cakeId) {
    let result = await axios.get(`/cakeshelves/cake/${cakeId}`);
    return util.jsonTransfer(result.data);
  }

  async updateReadStatus(id, readStatus) {
    let result = await axios.patch(`/cakeshelves/read-status/${id}`, {
      readStatus: readStatus,
    });
    return util.jsonTransfer(result.data);
  }

  async updateRanking(id, ranking) {
    let result = await axios.patch(`/cakeshelves/ranking/${id}`, {
      ranking: ranking,
    });
    return util.jsonTransfer(result.data);
  }

  async avgRankingByCakeId(cakeId) {
    let result = await axios.get(`/cakeshelves/average-ranking/${cakeId}`);
    return util.jsonTransfer(result.data);
  }
}

export default CakeShelfApi;
